package com.o2o.cleaning.month.platform.ebusiness_plat.kuaishou

import org.apache.spark.sql.SparkSession


object UpdateStandCategory {

  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .appName(s"${this.getClass.getSimpleName}")
      .config("spark.debug.maxToStringFields", "2000")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.caseSensitive", "true")
      .config("es.nodes", "192.168.1.29")
      .config("es.port", "9200")
      .config("cluster.name", "O2OElastic")
      .config("es.net.http.auth.user", "elastic")
      .config("es.net.http.auth.pass", "changeme")
      .config("es.index.read.missing.as.empty", "false")
      .master("local[*]")
      .getOrCreate()

    val sc = spark.sparkContext
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")
    sc.setLogLevel("ERROR")

    spark.udf.register("cate", cate _)

//    val csv_Df = spark.read.option("header", true).option("encoding", "utf-8").csv("D:\\o2o4zyfBywork\\需求\\快手直播\\8.csv")
    val csv_Df = spark.read.option("header", true).option("encoding", "utf-8").csv("D:\\o2o4zyfBywork\\需求\\每月平台数据清洗\\朴朴超市\\全量分类\\")

    csv_Df.registerTempTable("d")
    val df = spark.sql(
      """
        |select *,substr(s,1,5) firstCategoryId,subStr(s,1,7) secondCategoryId,
        | subStr(s,1,9) thirdCategoryId,s as fourthCategoryId from
        |(select *,cate(standId) s from d)
        |""".stripMargin)

//    df.coalesce(1).write.option("header", true).option("encoding", "utf-8").csv("D:\\o2o4zyfBywork\\需求\\快手直播\\stand_bu")
    df.coalesce(1).write.option("header", true).option("encoding", "utf-8").csv("D:\\o2o4zyfBywork\\需求\\每月平台数据清洗\\朴朴超市\\test\\")



  }

  // update   100110101   01
  def cate(col: String): String = {
    var colls = ""
    var fist = ""
    var second = ""
    var third = ""
    var fourth = ""
    if (col.length() == 2) {
      colls = "100" + col.concat("999999")
    } else if (col.length() == 4) {
      colls = "100" + col.concat("9999")
    } else if (col.length() == 6) {
      colls = "100" + col.concat("99")
    } else if (col.length() == 8) {
      colls = "100" + col
    } else {
      colls = "100" + col
    }
    colls
  }

}
